package service.aop;
//package tv.fuzegame.gameserver.aop;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.SortedMap;
//

//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import net.sf.json.JSONObject;
//
//import org.apache.log4j.Logger;
//import org.aspectj.lang.annotation.Aspect;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//
//import tv.fuzegame.common.util.StringTools;
//import tv.fuzegame.gameserver.util.Common;
//import tv.fuzegame.gameserver.util.Constant;
//import tv.fuzegame.gameserver.util.GameServerPropertiesCache;
//import tv.fuzegame.gameserver.util.LogUtil;
//
//@Aspect
//@Component
//public class SignInterceptor extends HandlerInterceptorAdapter {
//	
//    // 免检查地址 uncheckUrls 用一个单独的配置文件来配置
//    private List<String> uncheckUrls; 
//    
//    //需要检查的urls
//    private List<String> checkUrls; 
//
//    
//    private final static Logger log = org.apache.log4j.Logger
//			.getLogger(SignInterceptor.class);
//
//	
//	//拦截前处理
//	public boolean preHandle(HttpServletRequest request,
//			HttpServletResponse response, Object handler) throws Exception {
//		long start = System.currentTimeMillis();
//		checkUrls = GameServerPropertiesCache.getInstance().getCheckUrls();
//		uncheckUrls = GameServerPropertiesCache.getInstance().getUnCheckUrls();
//		String requestUrl = request.getRequestURI(); 
//		log.info("requestUrl: " + requestUrl);
//		log.info("uncheckUrls: " + uncheckUrls);
//		if ((!Common.contains(uncheckUrls, requestUrl)) || (Common.contains(checkUrls, requestUrl))) {
//			boolean isJsonRequest = false;
//			boolean isMultipart = false;
//			boolean md5IsRight = false;
//			String requestMD5 = "";
//			Map<String, String> headMap = Common.getHeadersInfo(request);
//			if(null!=headMap){
//				String contentType = headMap.get("content-type");
//				log.info("contentType：" + contentType);
//				if(null!=contentType&& ( contentType.indexOf("application/json")>=0 
//						||  contentType.indexOf("text/plain")>=0 )){
//					isJsonRequest = true;
//				}else if(null!=contentType&&contentType.indexOf("multipart/form-data")>=0){
//					isMultipart = true;
//				}
//				requestMD5 = headMap.get("content-md5"); 
//				if(null==requestMD5 || "".equals(requestMD5)){
//					md5IsRight = false;
//				}
//			}
//		
//			if(isJsonRequest){
//				String requestParamString = Common.getParamString(request); //获取请求参数
//				if (Common.validateSign(requestParamString.trim(), requestMD5)) {//验证签名通过
//					md5IsRight = true;
//				}
//			}else if(isMultipart){
//				String key  = GameServerPropertiesCache.getInstance().getProperties("fuzeGameServer", "key");
//				SortedMap<String, Object> sortedParams = Common.getQueryJsonString(request);//获取请求参数,并对map-key排升序
//				String generateMD5 = Common.generateSign(sortedParams, key);
//				if(null!=requestMD5 && requestMD5.equals(generateMD5)){
//					md5IsRight = true;
//				}
//			}
//			/*if(!md5IsRight){ //签名校验失败
//				Map<String, Object> resultMap = new HashMap<String, Object>();
//				int result = Constant.E_SYS_SIGN_NORIGHT;
//				resultMap.put("resultCode", result);
//				resultMap.put("serverResult", null);
//				response.getWriter().write(JSONObject.fromObject(resultMap).toString());
//				LogUtil.callLog(StringTools.obj2Json(requestMD5), Constant.E_SYS_SIGN_NORIGHT, System.currentTimeMillis() - start);
//				return false; 
//			}*/
//		}
//		//通过拦截
//		return true;
//	}	
//
//	// 拦截后处理
//	public void postHandle(HttpServletRequest request,
//			HttpServletResponse response, Object handler,
//			ModelAndView modelAndView) throws Exception {
//	}
//
//	// 全部完成后处理
//	public void afterCompletion(HttpServletRequest request,
//			HttpServletResponse response, Object handler, Exception ex)
//			throws Exception {
//	}
//
//}